﻿using B.S.ProcessEgine.API.Applocations.Commands.FormInfoCommand;
using B.S.ProcessEgine.API.Dtos.RequestDto;
using B.S.ProcessEgine.Domain.Entities;
using B.S.ProcessEgine.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace B.S.ProcessEgine.API.Controllers
{
    /// <summary>
    /// 表单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FormController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FormController> logger;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public FormController(IMediator mediator, ILogger<FormController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 表单添加
        /// </summary>
        /// <param name="formTypeInfo">表单实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ErrorCode.ApiResult> AddFormInfo(FormInfo form)
        {
            try
            {
                AddFormInfoCommand cmd = new AddFormInfoCommand()
                {
                    info = form
                };
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("表单添加出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 表单列表 + 查询
        /// </summary>
        /// <param name="cmd">查询条件</param>
        /// <returns>返回符合条件的对象</returns>
        [HttpGet]
        public async Task<ErrorCode.ApiResult<ErrorCode.ApiPaging<FormResponseDto>>> PagingFormInfo([FromQuery] GetFormListCommand cmd)
        {
            try
            {
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("表单分页列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        ///  表单删除
        /// </summary>
        /// <param name="cmd">表单id</param>
        /// <returns>返回受影响行数</returns>
        [HttpDelete]
        public async Task<ErrorCode.ApiResult> DelFormInfo([FromQuery] DeleteFormCommand cmd)
        {
            try
            {
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("删除表单出错" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 表单修改
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ErrorCode.ApiResult> UpdateFormInfo(FormInfo form)
        {
            try
            {
                FormUpdateCommand cmd = new FormUpdateCommand()
                {
                    Info = form
                };
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("删除表单出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 表单列表
        /// </summary>
        /// <returns>返回符合条件的对象</returns>
        [HttpGet]
        public async Task<ApiResult<IList<Domain.Entities.FormInfo>>> GetForm(int TypeId)
        {
            try
            {

                GetFormCommand command = new GetFormCommand
                {
                     TypeId=TypeId
                };
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("表单列表出错" + ex.Message);
                throw;
            }
        }




    }
}
